﻿//给定一个字符串数组strs，再给定两个字符串str1和str2，
//返回在strs中str1和str2的最小距离，
//如果str1或str2为null，或不在strs中，返回 - 1

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;
    string s1, s2;
    string s;
    cin >> n;
    cin >> s1 >> s2;
    int prev1 = -1, prev2 = -1, ret = 0x3f3f3f3f;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        if (s == s1) // 去前⾯找最近的 s2
        {
            if (prev2 != -1)
            {
                ret = min(ret, i - prev2);
            }
            prev1 = i;
        }
        else if (s == s2) // 去前⾯找 s1
        {
            if (prev1 != -1)
            {
                ret = min(ret, i - prev1);
            }
            prev2 = i;
        }
    }
    if (ret == 0x3f3f3f3f) cout << -1 << endl;
    else cout << ret << endl;
    return 0;
}